<?php
# 输入输出
namespace app\server;

use Yii;
use app\logic\BaseLogic;

class Api extends Server
{
    // 输出数据
    static public function json($data)
    {
        $status = array();
        $status['code'] = 1;
        $status['msg'] = 'SUCCESS';

        $response = array();
        $response['route'] = self::getRoute();
        list($s1, $s2) = explode(' ', microtime());
        $response['time'] = (float)sprintf('%.0f', (floatval($s1) + floatval($s2)) * 1000);
        $response['status'] = $status;
        $response['data'] = $data;

        $returnData = array();
        $returnData['res_data'] = json_encode($response);
        // 计算响应时间
        $returnData['use_time'] = $response['time'] - BaseLogic::$req_time;
        // 请求结果
        $returnData['status'] = $status['code'];

        $where = array();
        $where['symbol'] = BaseLogic::$symbol;

        // 记录日志
        $systemInfo = parent::getSystem();
        $coreInfo = isset($systemInfo['core']) ? $systemInfo['core'] : [];
        if ($coreInfo['api_log']) {
            $request = Yii::$app->request;
            if ($request->isPost) {
                Log::updateLog($returnData, $where);
            }
        }

        echo json_encode($response);
        exit;
    }

    // 输出错误
    static public function error($code, $msg = '')
    {
        $status = array();
        $status['code'] = $code;

        $err = parent::getErr();
        if ($code && isset($err[$code])) {
            $status['msg'] = $msg . $err[$code];
        } else {
            $status['msg'] = $msg;
        }

        $response = array();
        $response['route'] = self::getRoute();
        list($s1, $s2) = explode(' ', microtime());
        $response['time'] = (float)sprintf('%.0f', (floatval($s1) + floatval($s2)) * 1000);
        $response['status'] = $status;

        $returnData = array();
        $returnData['res_data'] = json_encode($response);
        // 计算响应时间
        $returnData['use_time'] = $response['time'] - BaseLogic::$req_time;
        // 请求结果
        $returnData['status'] = $code;

        $where = array();
        $where['symbol'] = BaseLogic::$symbol;

        // 记录日志
        $systemInfo = parent::getSystem();
        $coreInfo = isset($systemInfo['core']) ? $systemInfo['core'] : [];
        if ($coreInfo['api_log']) {
            $request = Yii::$app->request;
            if ($request->isPost) {
                Log::updateLog($returnData, $where);
            }
        }

        echo json_encode($response);
        exit;
    }

    // 获取请求路由
    static public function getRoute()
    {
        $SERVER_NAME = isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : '';
        $SERVER_PORT = isset($_SERVER['SERVER_PORT']) ? $_SERVER['SERVER_PORT'] : '';
        $REQUEST_URI = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '';
        if ($SERVER_PORT == '80' || empty($SERVER_PORT)) {
            $route = $SERVER_NAME . $REQUEST_URI;
        } else {
            $route = $SERVER_NAME . ':' . $SERVER_PORT . $REQUEST_URI;
        }
        return $route;
    }
}